package com.qianfeng.smartdevices.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevices.dto.R;
import com.qianfeng.smartdevices.pojo.Device;
import com.qianfeng.smartdevices.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * Created by jackiechan on 2020/12/13 15:00
 *
 * @Author jackiechan
 */
@RestController
@RequestMapping("/devices")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/device")
    public R AddDevice(@RequestBody Device device) {
        deviceService.AddDevice(device);
        return R.setOK(null);
    }

    @PutMapping("/device")
    public R updateDevice(@RequestBody Device device) {
        deviceService.updateDevice(device);
        return R.setOK(null);
    }

    @GetMapping("/device/{id}")
    public R getDeviceById(@PathVariable Long id) {
        Device device = deviceService.getDeviceById(id);
        return R.setOK(device);
    }

    @GetMapping("/devices")
    public R getAllDevices(@RequestParam(defaultValue = "1",required = false) int page, @RequestParam(required = false,defaultValue = "10") int limit,@RequestParam(required = false)  String address,   @RequestParam(required = false,defaultValue = "-100") Integer status,   @RequestParam(required = false,defaultValue = "-100")  int direction,   @RequestParam(required = false,defaultValue = "-100") int areaid,  @RequestParam(required = false,defaultValue = "-100") int categoryid) {
        PageInfo<Device> pageInfo = deviceService.getAllDevices(page, limit, address, status, direction,areaid,categoryid);
        return R.setOK(pageInfo);
    }

    @DeleteMapping("/devices")
    public R deleteDevices(@RequestParam String[] ids) {
        deviceService.deleteDevices(Arrays.asList(ids));
        return R.setOK(null);
    }

    /**
     * @param uuid 设备编号,也是我们 websocket 的唯一标识
     * @param command //命令
     */
    @PostMapping("/command/{uuid}/{command}")
    public R sendCommand(@PathVariable  String uuid, @PathVariable  String command) {
        deviceService.sendCommand(uuid, command);
        return R.setOK();
    }


}
